package com.example.springboott1.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboott1.entity.dto.AdminUserDTO1;
import com.example.springboott1.entity.AdminUser;
import com.example.springboott1.service.AdminUserService;
import com.example.springboott1.util.JwtUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminUserController {
    @Autowired
    private AdminUserService adminUserService;

//    @DeleteMapping("/delalldataofpeople")
//    public boolean delall(@RequestBody String ids){
//        JSONObject json = JSONObject.fromObject(ids);
//        JSONArray array = json.getJSONArray("ids");
//        List<String> idList = new ArrayList();
//        for (int i=0;i<array.size();i++){
//            idList.add(array.getString(i));
//        }
//        return adminUserService.removeByIds(idList);
//    }

    //新增
    @PostMapping("/")
    public boolean save(@RequestBody AdminUser adminUser) {
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", adminUser.getUsername());
        Long count = adminUserService.count(queryWrapper);
        if (count > 0) {
            return false;
        }
        //新增
        return adminUserService.save(adminUser);
    }

    //登录
    @PostMapping("/login")
    public AdminUserDTO1 login(@RequestBody AdminUser adminUser) {
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", adminUser.getUsername()).eq("password", adminUser.getUserpws());
        AdminUser adminUser11 = adminUserService.getOne(queryWrapper);

        //登录时加入token
        AdminUserDTO1 adminUserDTO1 = new AdminUserDTO1();
        if (adminUser11 != null && adminUser11.getId() != null) {
            Map<String, Object> payload = new HashMap<>();
            payload.put("id", adminUser11.getId());
            String token = JwtUtil.createToken(payload);

            adminUserDTO1.setAdminUser(adminUser11);
            adminUserDTO1.setToken(token);
        }
        return adminUserDTO1;
    }

    //验证token自动登录
    @PostMapping("/getToken")
    public boolean getToken(@PathVariable String token) {
        return true;
    }

    //分页，带查询参数
    @GetMapping("/page")
    public IPage<AdminUser> page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "100") Integer pageSize, @RequestParam(required = false) String zcname, @RequestParam Integer id) {
        IPage<AdminUser> page = new Page<>(pageNum, pageSize);
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        if (zcname != null) {
            queryWrapper.like("nickname", zcname);
            queryWrapper.ne("id", id);
        }
        return adminUserService.page(page, queryWrapper);
    }

//    @GetMapping("/findST")    //条件查询
//    public List<User> findByST(@RequestParam(required = false) String username, @RequestParam(required = false) String phone, @RequestParam(required = false) String address){
//        return userMapper.findST(username,phone,address);
//    }


    //删除
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
        return adminUserService.removeById(id);
    }


    //更新
    @PutMapping("/")
    public boolean update(@RequestBody String obj) {
        JSONObject jsons = JSONObject.fromObject(obj);
        JSONObject json = jsons.getJSONObject("obj");
        JSONArray arr = json.getJSONArray("arr");
        List<Integer> pmList = new ArrayList();
        for (int i = 0; i < arr.size(); i++) {
            pmList.add(arr.getInt(i));
        }
        int id = json.getInt("id");
        AdminUser adminUser = adminUserService.getById(id);
        adminUser.setPmone(false);
        adminUser.setPmtwo(false);
        adminUser.setPmthree(false);
        for (int i = 0; i < pmList.size(); i++) {
            if (pmList.get(i) == 1) {
                adminUser.setPmone(true);
            }
            if (pmList.get(i) == 2) {
                adminUser.setPmtwo(true);
            }
            if (pmList.get(i) == 3) {
                adminUser.setPmthree(true);
            }
        }

        return adminUserService.updateById(adminUser);
    }
//
//    //查询数据,可带参可不带
//    @GetMapping("/page")
//    public IPage<AdminUser> page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "13") Integer pageSize, @RequestParam( required = false) String username){
//        IPage<AdminUser> page = new Page<>(pageNum,pageSize);
//        QueryWrapper<AdminUser> queryWrapper=new QueryWrapper<>();
//        if (username!=null){
//            queryWrapper.like("username",username);
//        }
//        return adminUserService.page(page,queryWrapper);
//    }
//    @GetMapping("/findParam")
//    public List<AdminUser> findParam(@RequestParam(required = false) String username){
//        QueryWrapper<AdminUser> queryWrapper=new QueryWrapper<>();
//        if (username!=null){
//            queryWrapper.like("username",username);
//        }
//
//        return adminUserService.list(queryWrapper);
//    }
}
